﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.haoest.QrPortal
{
    public class Device
    {
        private string name;
        private bool isChecked;
        private string id;
        public static DeviceCollection devices = new DeviceCollection();

        public Device(string id, string name)
        {
            this.id = id;
            this.name = name;
            this.isChecked = false;
            Connected += SendSuccessMessageToDeviceClient;
            Connected += AddDevice;
            Disconnected += QuitDeviceClient;
            Disconnected += DeleteDevice;
            Renamed += UpdateDevice;

        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Id
        {
            get { return id; }
            set { }
        }

        public bool IsChecked
        {
            get { return isChecked; }
            set { isChecked = value; }
        }

        public delegate void DeviceEventHandler(Object sender, DeviceEventArgs e);
        public event DeviceEventHandler Disconnected = null;
        public event DeviceEventHandler Connected = null;
        public event DeviceEventHandler Renamed = null;

        public void onDisconnected(DeviceEventArgs e)
        {
            if (Disconnected != null)
            {
                Disconnected(this, e);
            }
        }

        public void onConnected(DeviceEventArgs e)
        {
            if (Connected != null)
            {
                Connected(this, e);
            }
        }

        public void onRenamed(DeviceEventArgs e)
        {
            if (Renamed != null)
            {
                Renamed(this, e);
            }
        }

        public class DeviceEventArgs : EventArgs
        {
            public readonly string id;
            public string name;
            public DeviceEventArgs(string id, string name)
            {
                this.id = id;
                this.name = name;
            }
        }

        private void SendSuccessMessageToDeviceClient(Object sender, DeviceEventArgs e)
        {

        }

        private void AddDevice(Object sender, DeviceEventArgs e)
        {
            devices.Add(this);
        }

        private void DeleteDevice(Object sender, DeviceEventArgs e)
        {
            devices.Remove(this);
        }

        private void QuitDeviceClient(Object sender, DeviceEventArgs e)
        {

        }

        private void UpdateDevice(Object sender, DeviceEventArgs e)
        {
            devices.Update(this);
        }

        public void Connect()
        {
            //这里写连接的方法
            DeviceEventArgs e = new DeviceEventArgs(this.id, this.name);
            onConnected(e);
        }

        public void Disconnect()
        {
            //这里写断开的方法
            DeviceEventArgs e = new DeviceEventArgs(this.id, this.name);
            onDisconnected(e);
        }

        public void Rename(string deviceName)
        {
            this.name = deviceName;
            DeviceEventArgs e = new DeviceEventArgs(this.id, this.name);
            onRenamed(e);
        }

    }
}
